package insight.Builder;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huu
 * @create 2025-08-26-19:00
 */
public class UpdateBuilder implements SQL.setStage,SQL.whereStage{
    private String tableName;
    private Map<String, Object> updateColumns = new LinkedHashMap<>();
    private String where;
    UpdateBuilder(String tableName){
        this.tableName = tableName;
    }
    @Override
    public SQL.whereStage set(String column, String value){
        updateColumns.put(column, value);
        return this;
    }
    public SQL.whereStage where(String where){
        this.where = where;
        return this;
    }

    @Override
    public String build(){
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE ");
        sql.append(tableName);
        sql.append(" SET ");
        //流又来了
        String collect = updateColumns.entrySet()
                .stream()
                .map(entry -> entry.getKey() + " = " + entry.getValue())
                .collect(Collectors.joining(","));
        sql.append(collect);
        sql.append(" WHERE ").append(where);
        return sql.toString();
    }
}